﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using WPFExtensions.Controls;

namespace Metis.Controls
{
    public partial class SplitControl //TODO: Add DP's for line width, handler size, line and polygon color.
    {
        #region Constructor

        public SplitControl()
        {
            InitializeComponent();
            Loaded += WindowLoaded;
        }

        #endregion

        #region Events

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            var adorner = new SplitAdorner(image1);
            var layer = AdornerLayer.GetAdornerLayer(image1);
            layer.Add(adorner);
        }

        #endregion

        #region DPs

        public static readonly DependencyProperty MiddleProperty =
            DependencyProperty
                .Register("Middle", typeof (double),
                          typeof (SplitControl),
                          new PropertyMetadata(0.0, SetMiddle));

        private static void SetMiddle(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            o.SetValue(MiddleProperty, (double) e.NewValue);
        }

        public double Middle
        {
            get { return (double)GetValue(MiddleProperty); }
            set { SetValue(MiddleProperty, value); }
        }

        public static readonly DependencyProperty ThetaProperty =
            DependencyProperty
                .Register("Theta", typeof (double),
                          typeof (SplitControl),
                          new PropertyMetadata(0.0, SetTheta));

        private static void SetTheta(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var element = o as SplitControl;
            if (element != null)
            {
                element.SetValue(ThetaProperty, (double) e.NewValue);
                element.image1.SetValue(ThetaProperty, e.NewValue);//SplitImage.ThetaProperty, e.NewValue);
            }
        }

        public double Theta
        {
            get { return (double)GetValue(ThetaProperty); }
            set { SetValue(ThetaProperty, value); }
        }

        public static readonly DependencyProperty SizeProperty =
            DependencyProperty.Register("Size", typeof (Size), 
            typeof (SplitControl), new PropertyMetadata(default(Size), SetSize));

        public Size Size
        {
            get { return (Size) GetValue(SizeProperty); }
            set { SetValue(SizeProperty, value); }
        }

        private static void SetSize(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as SplitControl;
            if (element != null)
            {
                d.SetValue(SizeProperty, (Size)e.NewValue);

                element.canvas1.Width = ((Size) e.NewValue).Width;
                element.canvas1.Height = ((Size) e.NewValue).Height;

                element.image1.Width = ((Size) e.NewValue).Width;
                element.image1.Height = ((Size) e.NewValue).Height;
            }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source",
                                        typeof (ImageSource), typeof (SplitControl),
                                        new PropertyMetadata(
                                            default(ImageSource),
                                            (o, e) =>
                                                {
                                                    var el = ((SplitControl) o);
                                                    el.image1.Source = (ImageSource) e.NewValue;
                                                    el.zoomControl.Mode = ZoomControlModes.Fill;
                                                }));

        public ImageSource Source
        {
            get { return (ImageSource)GetValue(Image.SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        #endregion
    }
}
